<template>
  <div class="app-container">
    <!-- 顶部查询 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="年度" prop="nd">
        <el-select filterable v-model="queryParams.nd" placeholder="请选择项目年度" clearable size="mini">
          <el-option
            v-for="dict in dict.type.year"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文件名称" prop="wjName">
        <el-input
          size="mini"
          v-model="queryParams.wjName"
          placeholder="请输入文件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批准文号" prop="pzWh">
        <el-input
          size="mini"
          v-model="queryParams.pzWh"
          placeholder="请输入批准文号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批准单位" prop="pzDw">
        <el-select filterable v-model="queryParams.pzDw" placeholder="请选择批准单位" clearable size="mini">
          <el-option
            v-for="dict in dict.type.xmgl_lxwj_pzdw"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['lxwj:lxwj:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['lxwj:lxwj:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['lxwj:lxwj:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="vxe-icon-file-excel"
          size="mini"
          @click="handleExport"
          v-hasPermi="['lxwj:lxwj:export']"
        >导出Excel
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              v-loading="loading" :data="lxwjList" @selection-change="handleSelectionChange"
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}">
<!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" width="50"/>
      <el-table-column label="年度" align="center" prop="nd" width="120"/>
      <el-table-column label="文件名称" align="center" prop="wjName" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.wjName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="批准文号" align="center" prop="pzWh" show-overflow-tooltip width="180"/>
      <el-table-column label="批准单位" align="center" prop="pzDw" show-overflow-tooltip width="200">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xmgl_lxwj_pzdw" :value="scope.row.pzDw"/>
        </template>
      </el-table-column>
      <el-table-column label="填报人" align="center" prop="createBy" width="130"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['lxwj:lxwj:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['lxwj:lxwj:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改立项文件对话框 -->
    <vxe-modal :title="title" v-model="open" width="65%" append-to-body showFooter show-zoom resize
               @close="canSaveIt = false">
      <el-form ref="formDialog" :model="form" :rules="rules" label-width="93px">
        <div style="height: 75vh; ">
          <!-- 基本信息 -->
          <el-card class="box-card">
            <div slot="header" class="clearfix" style="text-align: center;">
              <span style="font-size: 20px; color: #1890ff">基本信息</span>
            </div>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="年度：" prop="nd">
                  <el-input type="number" v-model.number="form.nd" placeholder="请输入年度"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="文件名称：" prop="wjName">
                  <el-input v-model="form.wjName" placeholder="请输入文件名称"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="批准文号：" prop="pzWh">
                  <el-input v-model="form.pzWh" placeholder="请输入批准文号"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="批准单位：" prop="pzDw">
                  <el-select v-model="form.pzDw" placeholder="请选择批准单位">
                    <el-option
                      v-for="dict in dict.type.xmgl_lxwj_pzdw"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="备注：" prop="bz">
              <el-input v-model="form.bz" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-card>
          <br>

          <!-- 课题信息 -->
          <el-card class="box-card">
            <div slot="header" class="clearfix" style="text-align: center;">
              <span style="font-size: 20px; color: #1890ff">课题信息</span>
            </div>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddLxglLxwjKtxx()">添加
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteLxglLxwjKtxx">删除
                </el-button>
              </el-col>
            </el-row>
            <el-table :data="lxglLxwjKtxxList" :row-class-name="rowLxglLxwjKtxxIndex"
                      @selection-change="handleLxglLxwjKtxxSelectionChange" ref="lxglLxwjKtxx">
              <el-table-column type="selection" width="50" align="center"/>
              <el-table-column label="序号" align="center" prop="index" width="50"/>
              <el-table-column label="年度" align="center" prop="nd" width="100"/>
              <el-table-column label="课题编号" align="center" prop="ktBh"/>
              <el-table-column label="课题名称" align="center" prop="ktName">
                <template slot-scope="scope">
                  <el-button
                    type="text"
                    @click="handleKtNameButtonClick(scope.row)"
                  >
                    <el-link type="primary">{{ scope.row.ktName }}</el-link>
                  </el-button>
                </template>
              </el-table-column>
              <el-table-column label="课题归属" align="center" prop="ktGs"/>
              <el-table-column label="申报单位" align="center" prop="zrBm" show-overflow-tooltip/>
              <el-table-column label="项目总额(万元)" align="center" prop="xmze">
                <template slot-scope="scope">
                  <el-button
                    type="text"
                    @click="xmZeButtonClick(scope.row)"
                  >
                    <el-link type="primary">{{ scope.row.xmze }}</el-link>
                  </el-button>
                </template>
              </el-table-column>
              <el-table-column label="开始时间" align="center" prop="qTime"/>
              <el-table-column v-if="1 === 2" label="项目申报ID" align="center" prop="xmsbId"/>
            </el-table>
          </el-card>
          <br>

          <!-- 相关附件 -->
          <el-card class="box-card">
            <div slot="header" class="clearfix" style="text-align: center;">
              <span style="font-size: 20px; color: #1890ff">相关附件</span>
            </div>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <!-- 上传按钮 -->
                <el-button size="mini" icon="el-icon-upload" type="primary" @click="lxwjFjShangChuan">上传</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteLxglLxwjFj">删除
                </el-button>
              </el-col>
            </el-row>
            <el-table style="min-height: 20vh" :data="lxglLxwjFjList" :row-class-name="rowLxglLxwjFjIndex"
                      @selection-change="handleLxglLxwjFjSelectionChange" ref="lxglLxwjFj">
              <el-table-column type="selection" width="50" align="center"/>
              <el-table-column label="序号" align="center" prop="index" width="50"/>
              <el-table-column show-overflow-tooltip label="文件名称" align="center" prop="fileName"
                               :formatter="getFileNames"/>
              <el-table-column label="文件大小" align="center" prop="fileSize" width="200">
              </el-table-column>
              <el-table-column label="操作" align="center" width="250">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleDownload(scope.row)"
                  >下载
                  </el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-view"
                    @click="handlePreview(scope.row)"
                  >预览
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-card>

        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </vxe-modal>

    <!-- 附件上传弹窗 -->
    <vxe-modal title="附件上传" v-model="fjShangChuan" width="25%" append-to-body showFooter @close="fjClose">
      <el-upload
        align="center"
        class="upload-demo"
        multiple
        :action="uploadFileUrl"
        :before-upload="handleBeforeUpload"
        :file-list="fileList"
        :limit="20"
        :on-error="handleUploadError"
        :on-exceed="handleExceed"
        :on-success="handleUploadSuccess"
        :headers="headers"
        ref="fileUpload"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">
          请上传
          <template> 大小不超过 <b style="color: #f56c6c">{{ 50 }}MB</b></template>
          <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b></template>
          的文件
        </div>
      </el-upload>

      <div slot="footer" class="dialog-footer" style="display: flex; justify-content: center;">
        <el-button v-if="allFilesUploaded === false" size="mini" type="primary" @click="fjClose">关 闭
        </el-button>
        <el-button v-if="allFilesUploaded === true" size="mini" type="primary" @click="fjClose">上传完成, 关闭
        </el-button>
      </div>
    </vxe-modal>

    <!-- 课题选择组件 -->
    <kt-xz ref="ktXz"></kt-xz>

    <!-- 立项文件详情组件 -->
    <lx-wj-xq ref="lxWjXqPicture"></lx-wj-xq>

    <!-- 查询项目申报(主表 + 子表) 弹窗 -->
    <Xmxq ref="xmxq"></Xmxq>

    <!-- 查看项目预算 弹窗 -->
    <XmZe ref="xmze"></XmZe>

    <!-- 预览组件 -->
    <KkFileView ref="preview"></KkFileView>

  </div>
</template>

<script>
import {
  addLxwj, delFj,
  delLxwj,
  getLxwj,
  getLxwjId,
  insertLxglLxwjFjByLxwjId,
  listLxwj,
  selectLxglLxwjFjList,
  selectLxwjLxxxByLxwjId,
  updateLxwj
} from "@/api/xmgl/lxgl/lxwj/lxwj";
import {getToken} from "@/utils/auth";
import KtXz from "@/views/components/ktXz/ktXz";
import LxWjXq from "@/views/xmgl/lxgl/xmcx/components/lxWjXq";
import Xmxq from "@/views/xmgl/lxgl/xmsb/components/xmxq";
import XmZe from "@/views/xmgl/lxgl/xmsb/components/xmZe";
import KkFileView from "@/views/components/KKFileView/kkFileView";

export default {
  name: "Lxwj",
  components: {KkFileView, XmZe, Xmxq, LxWjXq, KtXz},
  dicts: ['xmgl_lxwj_pzdw', 'kylx_ktgs', 'year'],
  provide() {
    return {
      updateKtXzLabel: (val) => {
        val.forEach(item => {
          // 检查是否已存在相同的 xmsbId
          const exists = this.lxglLxwjKtxxList.some(existingItem => existingItem.xmsbId === item.xmsbId);
          if (!exists) {
            this.lxglLxwjKtxxList.push(item);
          }
        });
      },
    }
  },
  data() {
    return {
      selectedRow: null, // 保存当前选中的行
      receivedKtXzLabel: {}, //从课题选择组建中选择到的课题信息List
      canSaveIt: false, // 点击新增后是否保存了基本信息
      allFilesUploaded: false, // 添加这个来追踪所有文件是否上传完成
      xg: false, //判断是新增还是修改 (false新增 true修改)
      fjShangChuan: false, //附件上传弹窗
      //要修改的 立项文件id
      lxwjIdEdit: "",
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      fileList: [],
      headers: {Authorization: "Bearer " + getToken()},
      number: 0,
      uploadList: [],
      fileType: ["doc", "xls", "ppt", "txt", "pdf", "docx", "zip"],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedLxglLxwjFj: [],
      // 课题信息子表选中数据
      checkedLxglLxwjKtxx: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 立项文件表格数据
      lxwjList: [],
      // 立项文件附件表格数据
      lxglLxwjFjList: [],
      // 立项文件-课题信息表格数据
      lxglLxwjKtxxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nd: null,
        wjName: null,
        pzWh: null,
        pzDw: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nd: [{required: true, message: "年度不能为空", trigger: "blur"}],
        wjName: [{required: true, message: "文件名称不能为空", trigger: "blur"}],
        pzWh: [{required: true, message: "批准文号不能为空", trigger: "blur"}],
        pzDw: [{required: true, message: "批准单位不能为空", trigger: "change"}],
      },
      getLxwjId: null, //从后端获得的立项文件UUID主键ID
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 打开预览组件
    handlePreview(row) {
      this.$refs.preview.handleOpen(row);
    },
    // 查看项目详情
    handleKtNameButtonClick(row) {
      this.$refs.xmxq.handleOpen(row);
    },
    // 查询项目总额信息(打开项目总额弹窗)
    xmZeButtonClick(row) {
      this.$refs.xmze.handleOpen(row);
    },
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 如果点击的行已经被选中，则取消选中
      if (selectedRows.includes(row)) {
        table.toggleRowSelection(row);
      } else {
        // 否则取消之前选中的行，并选中当前点击的行
        selectedRows.forEach(selectedRow => {
          table.toggleRowSelection(selectedRow);
        });
        table.toggleRowSelection(row);
      }
    },
    // 查询立项文件详情
    handleButtonClick(row) {
      this.$refs.lxWjXqPicture.handleOpen(row);
    },
    // 文件下载处理
    handleDownload(row) {
      const resource = row.fileName;
      // 默认方法
      this.$download.resource(resource);
    },
    //关闭上传附件弹窗后
    fjClose() {
      this.fileList = []
      this.allFilesUploaded = false
      this.fjShangChuan = false
    },
    // 获取文件名原名
    getFileNames(row) {
      // 从路径中提取文件名
      return row.fileName.split('/').pop(); // 返回文件名部分
    },
    lxwjFjShangChuan() { //可研性报告上传弹窗
      if (this.canSaveIt === false) {
        this.$modal.alertWarning("请先保存基本信息");
      } else {
        this.fjShangChuan = true;
      }
    },
    //上传文件之前的钩子
    handleBeforeUpload(file) {
      let that = this;
      // 校检文件类型
      if (this.fileType) {
        const fileName = file.name.split('.');
        const fileExt = fileName[fileName.length - 1];
        const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
        if (!isTypeOk) {
          this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
          return false;
        }
        // 校检文件大小
        if (this.fileSize) {
          const isLt = file.size / 1024 / 1024 < this.fileSize;
          if (!isLt) {
            this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
            return false;
          }
        }
        this.$modal.loading("正在上传文件，请稍候...");
        this.number++;
        return true;
      }
    },
    // 对象转成指定字符串分隔
    listToString(list, separator) {
      let strs = "";
      separator = separator || ",";
      for (let i in list) {
        strs += list[i].url + separator;
      }
      return strs !== '' ? strs.substr(0, strs.length - 1) : '';
    },
    // 上传失败
    handleUploadError() {
      this.$modal.msgError("上传文件失败，请重试");
      this.$modal.closeLoading();
    },
    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
    },
    // 上传成功
    handleUploadSuccess(res, file) {
      let that = this;
      if (res.code === 200) {
        this.uploadList.push({name: res.fileName, url: res.fileName, size: that.formatFileSize(file.size)});
        let xzItemFile = {
          "lxwjId": that.getLxwjId,
          "fileName": res.fileName,
          "fileSize": that.formatFileSize(file.size)
        };
        let xgItemFile = {
          "lxwjId": that.lxwjIdEdit,
          "fileName": res.fileName,
          "fileSize": that.formatFileSize(file.size)
        };
        if (this.xg === false) {
          //点击新增按钮的上传
          insertLxglLxwjFjByLxwjId(xzItemFile).then(resFil => {
            that.getLxwjFjList();
          })
        } else if (this.xg === true) {
          //点击修改按钮的上传
          insertLxglLxwjFjByLxwjId(xgItemFile).then(resFil => {
            that.getLxwjFjList();
          })
        }
        this.uploadedSuccessfully();
      } else {
        this.number--;
        this.$modal.closeLoading();
        this.$modal.msgError(res.msg);
        this.$refs.fileUpload.handleRemove(file);
        this.uploadedSuccessfully();
      }
      this.allFilesUploaded = true
      this.$modal.msgSuccess("上传成功")
      // this.fjShangChuan = false
    },
    getLxwjFjList() {
      let that = this;
      if (that.xg === false) {
        //点击新增 上传文件后的文件列表
        selectLxglLxwjFjList(that.getLxwjId).then(resFileList => {
          that.lxglLxwjFjList = resFileList.data;
        })
      } else {
        selectLxglLxwjFjList(that.lxwjIdEdit).then(resFileList => {
          that.lxglLxwjFjList = resFileList.data;
        })
      }
    },
    // 显示文件大小为KB, MB, GB, TB
    formatFileSize(size) {
      if (size === 0) return '0 Bytes';
      const k = 1024;
      const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
      const i = Math.floor(Math.log(size) / Math.log(k));
      return parseFloat((size / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
    },
    // 获取文件名称
    getFileName(name) {
      // 如果是url那么取最后的名字 如果不是直接返回
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1);
      } else {
        return name;
      }
    },
    // 上传结束处理
    uploadedSuccessfully() {
      if (this.number > 0 && this.uploadList.length === this.number) {
        this.fileList = this.fileList.concat(this.uploadList);
        this.uploadList = [];
        this.number = 0;
        this.$emit("input", this.listToString(this.fileList));
        this.$modal.closeLoading();
      }
    },
    /** 查询立项文件列表 */
    getList() {
      this.loading = true;
      listLxwj(this.queryParams).then(response => {
        this.lxwjList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.canSaveIt = false
    },
    // 表单重置
    reset() {
      this.form = {
        lxwjId: this.getLxwjId,
        nd: new Date().getFullYear(),
        wjName: null,
        pzWh: null,
        pzDw: null,
        bz: null,
        idWhere: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null
      };
      this.lxglLxwjFjList = [];
      this.lxglLxwjKtxxList = [];
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.lxwjId)
      if (ids.length === 1) {
        this.ids = ids[0]
      }else {
        this.ids = ids
      }
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.xg = false
      getLxwjId().then(res => {
        this.getLxwjId = res.msg
        this.reset();
      })
      this.open = true;
      this.title = "添加立项文件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.canSaveIt = true
      this.xg = true
      this.reset();
      const lxwjId = row.lxwjId || this.ids
      this.lxwjIdEdit = row.lxwjId
      getLxwj(lxwjId).then(response => {
        this.form = response.data;
        this.lxglLxwjFjList = response.data.lxglLxwjFjList;
        this.open = true;
        this.title = "修改立项文件";
      });
      selectLxwjLxxxByLxwjId(lxwjId).then(res => {
        this.lxglLxwjKtxxList = res.data.lxglLxwjKtxxList;
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          this.form.lxglLxwjFjList = this.lxglLxwjFjList;
          this.form.lxglLxwjKtxxList = this.lxglLxwjKtxxList;
          if (this.form.idWhere != null) {
            updateLxwj(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.xg = false
            });
          } else {
            addLxwj(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.canSaveIt = true //已经新增了基本信息, 可以增加子表信息了
              this.getList();
              this.handleUpdate(this.form)
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const lxwjIds = row.lxwjId || this.ids;
      this.$modal.confirm('是否确认删除立项文件编号为"' + lxwjIds + '"的数据项？').then(function () {
        return delLxwj(lxwjIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 立项文件附件序号 */
    rowLxglLxwjFjIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 立项文件附件删除按钮操作 */
    handleDeleteLxglLxwjFj() {
      if (this.checkedLxglLxwjFj.length == 0) {
        this.$modal.msgError("请先选择要删除的立项文件附件数据");
      } else {
        const fjIds = this.checkedLxglLxwjFj;
        this.$modal.confirm('确定删除选中记录？').then(function() {
          return delFj(fjIds);
        }).then(() => {
          getLxwj(this.lxwjIdEdit).then(response => {
            this.lxglLxwjFjList = response.data.lxglLxwjFjList;
          });
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    },
    /** 复选框选中数据 */
    handleLxglLxwjFjSelectionChange(selection) {
      this.checkedLxglLxwjFj = selection.map(item => item.fjId)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lxwj/lxwj/export', {
        ...this.queryParams
      }, `lxwj_${new Date().getTime()}.xlsx`)
    },
    /** 立项文件-课题信息序号 */
    rowLxglLxwjKtxxIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 立项文件-课题信息添加按钮操作 */
    handleAddLxglLxwjKtxx() {
      if (this.canSaveIt === false) {
        this.$modal.alertWarning("请先保存基本信息");
      } else {
        this.$refs.ktXz.handleOpen(this.lxglLxwjKtxxList);
      }
    },
    /** 立项文件-课题信息删除按钮操作 */
    handleDeleteLxglLxwjKtxx() {
      if (this.checkedLxglLxwjKtxx.length === 0) {
        this.$modal.msgError("请先选择要删除的立项文件-课题信息数据");
      } else {
        const lxglLxwjKtxxList = this.lxglLxwjKtxxList;
        const checkedLxglLxwjKtxx = this.checkedLxglLxwjKtxx;
        this.lxglLxwjKtxxList = lxglLxwjKtxxList.filter(function (item) {
          return checkedLxglLxwjKtxx.indexOf(item.index) === -1
        });
      }
    },
    /** 复选框选中数据 */
    handleLxglLxwjKtxxSelectionChange(selection) {
      this.checkedLxglLxwjKtxx = selection.map(item => item.index)
    },
  }
};
</script>

<style scoped>
/* header 头部背景 */
/deep/ .el-card__header {
  padding: 3px 7px 3px;
  min-height: 10px;
  background-color: aliceblue;
}
</style>
